<?php class category extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function insert(){
       $this->name = $_POST['name'];
       $this->codes = $_POST['codes'];
       $this->mod_id = $_POST['mod_id'];
       $this->level = $_POST['level'];
       $this->createtime = time();
       $re = $this->select(array('mod_id'=>$_POST['mod_id'],'name'=>trim($_POST['name'])));
       if(count($re)==0){
         $result = $this->db->insert('category',$this);
       }else{
         $result=true;
       }
       return $result;
    }
    
    function auth($codes,$mod){
    	$this->db->where(array('codes'=>trim($codes),'mod_id'=>$_POST['mod_id']));
    	$this->db->from('category');
        $result = $this->db->count_all_results(); 
        return $result;
    }
    
    
    function getcategory_bymod_id($mod_id){
        $this->db->where(array('mod_id'=>$mod_id));
    	$query = $this->db->get('category');
        $result = $query->result(); 
        return $result;
    }
    
    
    function main_select_count($da,$type){
       if($type=="mod_id"||$type=="category_id"){	
            $sql = "select DISTINCT $type from category where ".$da ;
       }
       $query = $this->db->query($sql);
       $result = $query->result();
       return $result;
    }
    
    
    function main_select($array,$num="",$offset=""){

       $sql = "select * from category where ".$array ;
       if($num!=""){
         $sql.=" limit $offset,$num";
       }
       $query = $this->db->query($sql);
       $result = $query->result();
       return $result;
    }
    
    
    function select($array,$num="",$offset=""){
       $this->db->limit($num,$offset);	
       if($array!=""){
       	  $this->db->where($array);
       }
       $query = $this->db->get('category');
       $result = $query->result(); 
       return $result;
    }
    
    function update($id){
       $this->name = $_POST['name'];
       $this->codes = $_POST['codes'];
       $this->mod_id = $_POST['mod_id'];
       $this->level = $_POST['level'];
       $result = $this->db->update('category',$this,array('category_id'=>$id));
       return $result;
    }
    
    function del($id){
       $result = $this->db->delete('category',array('category_id'=>$id));
       return $result;
    }
}